package com.scada.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.scada.hessian.client.impl.LectureInfoVisitorClient;
import com.scada.hessian.client.impl.LectureMemberVisitorClient;
import com.scada.model.bean.LectureInfo;
import com.scada.model.bean.User;
import com.scada.model.bean.vo.LectureInfoVO;
import com.scada.model.dataTable.LectureChooseMemberList;
import com.scada.model.dataTable.LectureInfoList;
import com.scada.model.dataTable.LectureNotChooseMemberList;
import com.scada.model.result.PageInfo;
import com.scada.model.result.ResponseBean;
import com.scada.model.search.LectureChooseMemberSearch;
import com.scada.model.search.LectureInfoSearch;
import com.scada.model.search.LectureNotChooseMemberSearch;

@Controller
@RequestMapping("lecture")
public class LectureInfoController extends BaseController{

	@Autowired
	private LectureInfoVisitorClient lectureInfoVisitorClient;
	
	@Autowired
	private LectureMemberVisitorClient lectureMemberVisitorClient;
	
	
	@RequestMapping(value = "toLectureMain")
	public String toMain(){
		return "lecture/list";
	}
	
	@RequestMapping(value = "getLectureInfoPageData")
	@ResponseBody
	public String getLectureInfoPageData(@ModelAttribute LectureInfoSearch lectureInfoSearch) {
		
		PageInfo<LectureInfoList> pageInfo = new PageInfo<LectureInfoList>();
        pageInfo.setStart(lectureInfoSearch.getStart());
        pageInfo.setPageSize(lectureInfoSearch.getLength());
        pageInfo.setDraw(lectureInfoSearch.getDraw());
        ResponseBean rb = lectureInfoVisitorClient.getLectureInfoPageData(lectureInfoSearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<LectureInfoList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<LectureInfoList>());
        }
        
		return toJson(pageInfo);
	}
	
	
	@RequestMapping(value = "toAddLectureInfo")
	public ModelAndView toAddLectureInfo(ModelMap model){
		
		return new ModelAndView("lecture/add");
	}
	
	@RequestMapping(value = "insertLectureInfo")
	@ResponseBody
	public String insertLectureInfo(@ModelAttribute LectureInfo lectureInfo){
		
		User user = getCurrentUser();
		lectureInfo.setCreateId(user.getId());
		lectureInfo.setLastUpdateId(user.getId());
		
		ResponseBean result = new ResponseBean();
		int count = 0;
		ResponseBean valiRb = lectureInfoVisitorClient.validateNumRepeat(lectureInfo.getLectureNum(), null);
		if(StringUtils.equals("2", valiRb.getType())){
			count = (int)valiRb.getObject("result");
		}else {
			result.failed("验证讲座编号重复失败");
		}
		
		if(count == 0) {
			result = lectureInfoVisitorClient.insertLectureInfo(lectureInfo);
		}else {
			result.failed("讲座编号重复");
		}
		return toJson(result);
	}
	
	@RequestMapping(value = "toEditLectureInfo")
	public ModelAndView toEditLectureInfo(Long id, ModelMap model){
		
		LectureInfoVO li = null;
		try {
			ResponseBean rb = lectureInfoVisitorClient.getLectureInfoById(id);
			if(StringUtils.equals("2", rb.getType())){
				li = (LectureInfoVO)rb.getObject("result");
			}
		}catch (Exception e) {
			
		}
		model.put("li", li);
		return new ModelAndView("lecture/edit");
	}
	
	@RequestMapping(value = "updateLectureInfo")
	@ResponseBody
	public String updateLectureInfo(@ModelAttribute LectureInfo lectureInfo){
		
		User user = getCurrentUser();
		lectureInfo.setLastUpdateId(user.getId());
		
		ResponseBean result = new ResponseBean();
		int count = 0;
		ResponseBean valiRb = lectureInfoVisitorClient.validateNumRepeat(lectureInfo.getLectureNum(), lectureInfo.getId());
		if(StringUtils.equals("2", valiRb.getType())){
			count = (int)valiRb.getObject("result");
		}else {
			result.failed("验证讲座编号重复失败");
		}
		
		if(count == 0) {
			result = lectureInfoVisitorClient.updateLectureInfo(lectureInfo);
		}else {
			result.failed("讲座编号重复");
		}
		
		return toJson(result);
	}
	
	@RequestMapping(value = "deleteLectureInfoById")
	@ResponseBody
	public String deleteLectureInfoById(String ids){
		
		ResponseBean rb = new ResponseBean();
		try{
			lectureInfoVisitorClient.deleteLectureInfoById(LectureInfo.IsShowCode.NO.getKey(), ids);
			rb.success();
		}catch(Exception e){
			rb.failed();
		}
		return toJson(rb);
	}
	
	@RequestMapping(value = "toNotChooseMemberMain")
	public String toNotChooseMemberMain(Long lectureId, ModelMap model){
		
		model.put("lectureId", lectureId);
		return "lecture/notChooseMember";
	}
	
	@RequestMapping(value = "toChooseMemberMain")
	public String toChooseMemberMain(Long lectureId, ModelMap model){
		
		model.put("lectureId", lectureId);
		return "lecture/chooseMember";
	}
	
	@RequestMapping(value = "getNotChooseMemberPageData")
	@ResponseBody
	public String getNotChooseMemberPageData(@ModelAttribute LectureNotChooseMemberSearch lectureNotChooseMemberSearch) {
		
		PageInfo<LectureNotChooseMemberList> pageInfo = new PageInfo<LectureNotChooseMemberList>();
        pageInfo.setStart(lectureNotChooseMemberSearch.getStart());
        pageInfo.setPageSize(lectureNotChooseMemberSearch.getLength());
        pageInfo.setDraw(lectureNotChooseMemberSearch.getDraw());
        ResponseBean rb = lectureMemberVisitorClient.getNotChooseMemberPageData(lectureNotChooseMemberSearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<LectureNotChooseMemberList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<LectureNotChooseMemberList>());
        }
        
		return toJson(pageInfo);
	}
	
	@RequestMapping(value = "getChooseMemberPageData")
	@ResponseBody
	public String getChooseMemberPageData(@ModelAttribute LectureChooseMemberSearch lectureChooseMemberSearch) {
		
		PageInfo<LectureChooseMemberList> pageInfo = new PageInfo<LectureChooseMemberList>();
        pageInfo.setStart(lectureChooseMemberSearch.getStart());
        pageInfo.setPageSize(lectureChooseMemberSearch.getLength());
        pageInfo.setDraw(lectureChooseMemberSearch.getDraw());
        ResponseBean rb = lectureMemberVisitorClient.getChooseMemberPageData(lectureChooseMemberSearch);
        if(StringUtils.equals("2", rb.getType())){
        	pageInfo.setRecordsTotal((Integer)rb.getObject("resultCount"));
        	pageInfo.setData((List<LectureChooseMemberList>)rb.getObject("resultList"));
        }else{
        	pageInfo.setRecordsTotal(0);
        	pageInfo.setData(new ArrayList<LectureChooseMemberList>());
        }
        
		return toJson(pageInfo);
	}
	
	@RequestMapping(value = "saveMemberLecture")
	@ResponseBody
	public String saveMemberLecture(Long lectureId, String ids, Integer lectureType){
		
		LectureInfoVO li = null;
		ResponseBean result = new ResponseBean();
		boolean re = true;
		Integer memberNum = 0;
		try {
			ResponseBean rb = lectureInfoVisitorClient.getLectureInfoById(lectureId);
			if(StringUtils.equals("2", rb.getType())){
				li = (LectureInfoVO)rb.getObject("result");
			}else {
				result.failed("验证参与人数限制失败");
				re = false;
			}
			
			if(li.getLimitNum() != null) {
				ResponseBean rb1 = lectureMemberVisitorClient.getLectureMemberNumber(lectureId);
				if(StringUtils.equals("2", rb1.getType())){
					memberNum =  (Integer)rb1.getObject("result")+ids.split(",").length;
					if(li.getLimitNum().intValue() - memberNum < 0) {
						result.failed("参与人数已超过演讲限制人数");
						re = false;
					}
				}else {
					result.failed("验证参与人数限制失败");
					re = false;
				}
			}
			
		}catch (Exception e) {
			result.failed("验证参与人数限制失败");
		}
		
		if(re) {
			try{
				lectureMemberVisitorClient.insertLectureMember(ids, lectureId, lectureType);
				result.success();
			}catch(Exception e){
				result.failed();
			}
		}
		
		return toJson(result);
	}
	
	
	@RequestMapping(value = "deleteLectureMember")
	@ResponseBody
	public String deleteLectureMember(String ids){
		
		ResponseBean rb = new ResponseBean();
		try{
			lectureMemberVisitorClient.deleteLectureMemberById(ids);
			rb.success();
		}catch(Exception e){
			rb.failed();
		}
		return toJson(rb);
	}
	
}
